package com.prs.crm.action.stock;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreOut;
import com.prs.crm.domain.store.StoreOutDetail;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.stock.ProductService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StockAlertService;
import com.prs.crm.service.stock.StoreOutService;
import com.prs.crm.service.stock.StoreRoomService;
import com.prs.crm.service.sys.CodeInvoiceService;
import com.prs.crm.service.sys.CodeService;

public class StoreOutAction extends FlowAction<StoreOut> {
	private static final long serialVersionUID = 1L;
	
	private CodeInvoice codeInvoice;
	
	private CodeInvoiceService codeInvoiceService;
	
	private String Encode;
	
	private Code code;
	
	private CodeService codeService;
	
	private Collection<CodeInvoice> codelist;

	private String[] names;

	private Integer[] detailid;

	private StoreRoom[] rooms;

	private Product[] products;

	private StoreOut storeout;

	private Integer[] storeoutNums;

	private List<StoreRoom> storerooms;

	@Autowired
	private ProductStoreService productStoreService;

	private StoreOutService storeOutService;

	@Autowired
	private StoreRoomService storeRoomService;

	private PaginationSupport pagination;

	private Collection<StoreOutDetail> slist;

	private List<String> mapValidateStockAlert;

	private List<Product> productlist;

	private Map<String, Double> mapValidateOutMap;

	private StockAlertService stockAlertService;

	private StoreRoom storeRoom;

	private String batchNumber;

	private String[] products1;

	private String[] storenumsStrings;

	private ProductService productService;
	private String listString;
	private String flow;

	@FlowActive(target = "storeout", flowservice = "storeOutService")
	@Action("editStoreOut")
	public String editStoreOut() {
		this.storerooms = this.storeRoomService.getAllstoreroom();
		this.codelist  =this.getCodeInvoiceService().getInvoiceCodeType("出库单");
		return SUCCESS;
	}

	@Action(value = "changetype", results = { @Result(name = "success", type = "json", params = {
			"root", "states" }) })
	public String changeStoreout() {

		return SUCCESS;
	}

	@Action("selectProductStore")
	public String selectProductStore() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProductStore.class);
		criteria.add(Restrictions.eq("room", storeout.getRoom()));
		
		if(batchNumber!=null &&  (!batchNumber .equals(""))){
			   criteria.add(Restrictions.eq("batchNum",batchNumber));	
		}
		this.setPagination(this.getProductStoreService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(flowservice = "storeOutService", target = "storeout")
	@Action(value = "saveStoreOut", results = {
			@Result(name = "success", type = "redirect", location = "listStoreOutOder"),
			@Result(name = "error", type = "chain", location = "editStoreOut"),
			@Result(name = "none", type = "redirect", location = "listStoreOutOder") })
	public String saveStoreOut() {
		if(storeout.getStoreOutCode()==null||"".equals(storeout.getStoreOutCode().trim())){
			this.addActionError("编码不能为空");
			return ERROR;
		}
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码","库房出库",storeout.getStoreOutCode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		if(flow==null||flow.equals("")){
			this.addActionError("审核流程不能为空");
			return ERROR;
		}
		
		if (storeout.getTitlename() == null
				|| storeout.getTitlename().equals("")) {
			this.addActionError("出库主题不能为空");
			return ERROR;
		}
		if (storeout.getStoroutdata() == null) {
			this.addActionError("出库时间不能为空");
			return ERROR;
		}
		if (storeout.getBatchNum() == null || storeout.getBatchNum().equals("")) {
			this.addActionError("批号不能为空");
			return ERROR;
		}
		if (storeout.getRoom() == null) {
			this.addActionError("仓库不能为空");
			return ERROR;
		}
		if (products == null) {
			this.addActionError("出库明细不能为空");
			return ERROR;
		}
		/**
		 * 编码产生
		 */
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(storeout.getStoreOutCode());
		    codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			codes.setCodeMolde("库房出库");
			this.getCodeService().save(codes);
			storeout.setCode(codes);
			this.getStoreOutService().saveAll(storeout, rooms, products,storeoutNums, names, detailid);
		} else {
		CodeInvoice  Vob=this.getCodeInvoiceService().getVobject(codeInvoice.getId());
		Code codes=new Code();
		 codes.setEntityName(this.getClass().getName());
		codes.setCodeValue(storeout.getStoreOutCode());		
		Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
		this.getCodeInvoiceService().saveOrUpdate(Vob);
		codes.setCodeType(Vob.getCodePlan());
		codes.setCodeMolde("库房出库");
		this.getCodeService().save(codes);
		storeout.setCode(codes);
		this.getStoreOutService().saveAll(storeout, rooms, products,storeoutNums, names, detailid);
		}
		
		
		
		
		
		this.setPrompt("出库成功");
		return SUCCESS;
	}

	public static String Increase(String backupNum){
		String Inc="";
		int lengths=backupNum.length();
	    String lengthss=(Integer.parseInt(backupNum)+1)+"";
	    int increase=(lengthss.trim()).length();
	    if(lengths!=increase){
	    	String In="";
	    	for(int i=0;i<lengths-increase;i++){
	    		In=In+"0";
	    	}
	    	In=(In+lengthss).trim();
	    	Inc=In;
	    }else{
	    	Inc=lengthss;
	    }
	    return Inc.trim();
	}
	
	@Action("listStoreOutOder")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				StoreOut.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getStoreOutService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewStoreOut")
	public String viewStoreOut() {
		storeout = this.getStoreOutService().get(storeout.getId());
		this.slist = storeout.getOutdetails();
		this.displayEndNodes(storeout);
		return SUCCESS;
	}

	@Action(value = "validateStockAlert2", results = { @Result(name = "success", type = "json", params = {
			"root", "mapValidateStockAlert" }) })
	public String validateStockAlert2() {
		if (getProducts(products1) == null
				|| getNumbers(storenumsStrings) == null) {
			mapValidateStockAlert = null;
		} else {
			mapValidateStockAlert = this.getStockAlertService()
					.validateStockAlert(storeRoom, getProducts(products1));
			products1 = null;
		}
		return SUCCESS;
	}

	@Action(value = "validateOut", results = { @Result(name = "success", type = "json", params = {
			"root", "mapValidateOutMap" }) })
	public String validateOut() {
		if (getProducts(products1) == null
				|| getNumbers(storenumsStrings) == null) {
			mapValidateOutMap = null;
		} else {
			mapValidateOutMap = this.getStockAlertService()
					.validateOutStore(storeRoom, getProducts(products1),
							getNumbers(storenumsStrings),
							this.getProductStoreService());
			products1 = null;
			storenumsStrings = null;
		}

		return SUCCESS;
	}

	public Product[] getProducts(String[] is) {
		if (products==null) {
			return null;
		} else {
			Product[] productst = new Product[is[0].split(" ,").length];

			try {

				String[] productids = is[0].split(" ,");
				String tt = is[0].split(" ,")[is[0].split(" ,").length - 1];
				tt = tt.substring(0, tt.length() - 1);
				for (int i = 0; i < productids.length - 1; i++) {
					productst[i] = this.getProductService().get(
							Integer.parseInt(productids[i]));
				}
				productst[is[0].split(" ,").length - 1] = this
						.getProductService().get(Integer.parseInt(tt));

			} catch (Exception e) {
				e.printStackTrace();
			}
			return productst;
		}

	}

	public double[] getNumbers(String[] is) {
		if (products==null) {
			return null;
		} else {
			double[] integers1 = new double[is[0].split(" ,").length];
			try {
				String[] strings = is[0].split(" ,");
				String tt = is[0].split(" ,")[is[0].split(" ,").length - 1];
				tt = tt.substring(0, tt.length() - 1);
				for (int i = 0; i < strings.length - 1; i++) {
					integers1[i] = Double.valueOf(strings[i]);
				}
				integers1[is[0].split(" ,").length - 1] = Double.valueOf(tt);

			} catch (Exception e) {
				e.printStackTrace();
			}
			return integers1;
		}

	}

	public StoreOut getStoreout() {
		return storeout;
	}

	public void setStoreout(StoreOut storeout) {
		this.storeout = storeout;
	}

	public StoreOutService getStoreOutService() {
		return storeOutService;
	}

	public void setStoreOutService(StoreOutService storeOutService) {
		this.storeOutService = storeOutService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public List<StoreRoom> getStorerooms() {
		return storerooms;
	}

	public void setStorerooms(List<StoreRoom> storerooms) {
		this.storerooms = storerooms;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public Collection<StoreOutDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<StoreOutDetail> slist) {
		this.slist = slist;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

	public StoreRoom[] getRooms() {
		return rooms;
	}

	public void setRooms(StoreRoom[] rooms) {
		this.rooms = rooms;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public Integer[] getStoreoutNums() {
		return storeoutNums;
	}

	public void setStoreoutNums(Integer[] storeoutNums) {
		this.storeoutNums = storeoutNums;
	}

	public String[] getNames() {
		return names;
	}

	public void setNames(String[] names) {
		this.names = names;
	}

	public Integer[] getDetailid() {
		return detailid;
	}

	public void setDetailid(Integer[] detailid) {
		this.detailid = detailid;
	}

	public List<String> getMapValidateStockAlert() {
		return mapValidateStockAlert;
	}

	public void setMapValidateStockAlert(List<String> mapValidateStockAlert) {
		this.mapValidateStockAlert = mapValidateStockAlert;
	}

	public Map<String, Double> getMapValidateOutMap() {
		return mapValidateOutMap;
	}

	public void setMapValidateOutMap(Map<String, Double> mapValidateOutMap) {
		this.mapValidateOutMap = mapValidateOutMap;
	}

	public StockAlertService getStockAlertService() {
		return stockAlertService;
	}

	public void setStockAlertService(StockAlertService stockAlertService) {
		this.stockAlertService = stockAlertService;
	}

	public StoreRoom getStoreRoom() {
		return storeRoom;
	}

	public void setStoreRoom(StoreRoom storeRoom) {
		this.storeRoom = storeRoom;
	}

	public String[] getProducts1() {
		return products1;
	}

	public void setProducts1(String[] products1) {
		this.products1 = products1;
	}

	public String[] getStorenumsStrings() {
		return storenumsStrings;
	}

	public void setStorenumsStrings(String[] storenumsStrings) {
		this.storenumsStrings = storenumsStrings;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public String getBatchNumber() {
		return batchNumber;
	}

	public void setBatchNumber(String batchNumber) {
		this.batchNumber = batchNumber;
	}

	public List<Product> getProductlist() {
		return productlist;
	}

	public void setProductlist(List<Product> productlist) {
		this.productlist = productlist;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeInvoice> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeInvoice> codelist) {
		this.codelist = codelist;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

}
